//
//  MVVMDemo3ViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/1/7.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//

#import "MVVMDemo3ViewController.h"

#import "DataViewModel.h"


@interface MVVMDemo3ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) DataViewModel *dataViewModel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation MVVMDemo3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.navigationItem.title = @"ReactCocoa模式下的MVVM";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    [self bindData];
}

- (void)bindData
{
    self.dataViewModel = [DataViewModel new];
    /// 信号搭建好
    @weakify(self);
    
    [self.dataViewModel.successObject subscribeNext:^(id x) {
       
        @strongify(self);
        NSArray *array = x;
        self.dataSource = array;
        [self.tableView reloadData];
    }];
    
    /// 发信号
    [self.dataViewModel exchangeData];
}

#pragma mark --- <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [self.dataSource[indexPath.row] valueForKey:@"city"];
    return cell;
}

#pragma mark --- <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView的cell");
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [UIView new];
//        _tableView.separatorStyle = NO;
    }
    return _tableView;
}


@end
